package org.rabbitmq.topic.common.configuration;

import org.rabbitmq.topic.common.enums.ExchangeEnum;
import org.rabbitmq.topic.common.enums.QueueEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
*@author LSY
*Time: 2018年12月7日上午10:39:30
*
*
*/
@Configuration
public class UserRegisterQueueConfiguration {

	private Logger log = LoggerFactory.getLogger(UserRegisterQueueConfiguration.class);
	
	/**
	 * 配置用户注册主题
	 */
	@Bean
	public TopicExchange userTopicExchange() {
		TopicExchange topicExchange = new TopicExchange(ExchangeEnum.USER_REGISTER_TOPIC_EXCHANGE.getName());
		log.info("用户注册交换实例化成功");
		return topicExchange;
	}
	
	/**
	 * 配置用户注册
	 * 发送激活邮件消息队列
	 * 并设置持久化队列
	 * @return
	 */
	@Bean
	public Queue sendRegisterMailQueue() {
		Queue queue = new Queue(QueueEnum.USER_REGISTER_SEND_MAIL.getName());
		log.info("用户创建注册消息队列成功");
		return queue;
	}
	
	/**
	 * 配置注册用户
	 * 创建账户消息队列
	 * 并设置持久化队列
	 * 
	 * @return
	 */
	@Bean
	public Queue createAccountQueue() {
		Queue queue = new Queue(QueueEnum.USER_REGISTER_CREATE_ACCOUNT.getName());
		log.info("创建 用户注册账号队列成功");
		return queue;
	}
	
	/**
	 * 绑定队列 到 用户注册主题 交换配置
	 * @return
	 */
	@Bean
	public Binding sendMailBinding(TopicExchange userTopicExchange,Queue sendRegisterMailQueue) {
		Binding binding = BindingBuilder.bind(sendRegisterMailQueue).to(userTopicExchange).with(QueueEnum.USER_REGISTER_SEND_MAIL.getRoutingKey());
		log.info("绑定发送邮件到注册交换成功");
		return binding;
	}
	
	/**
	 * 绑定用户创建账户到用户注册主题交换配置
	 * @param userTopicExchange
	 * @param createAccountQueue
	 * @return
	 */
	@Bean
	public Binding createAccountBinding(TopicExchange userTopicExchange,Queue createAccountQueue) {
		Binding binding = BindingBuilder.bind(createAccountQueue).to(userTopicExchange).with(QueueEnum.USER_REGISTER_CREATE_ACCOUNT.getRoutingKey());
		log.info("绑定创建用户到注册交换成功");
		return binding;
	}
	
}
